/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package professor.bd;

import conexao.ConexaoSingleton;
import professor.classes.*;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 *
 * @author Gabi
 */
public class ProfessorDAO {

     private EntityManager manager;
     private Query query;

     public ProfessorDAO() throws Exception{
         manager = ConexaoSingleton.getConexao();
     }


     public Professor getProfessor(Integer codigo){
     Professor professor = manager.find(Professor.class, codigo);
     return professor;
     
     }
     public List<Professor> getProfessor(){
     query = manager.createQuery("select p from Professor p order by nome");
     return query.getResultList();
     }

     public void salvar(Professor professor){
         manager.getTransaction().begin();
         manager.persist(professor);
         manager.getTransaction().commit();
     }
     public void excluir(Professor professor){
         manager.getTransaction().begin();
         manager.remove(professor);
         manager.getTransaction().commit();
     }

     public List<Professor> pesquisaProfessor(String professor){
         query = manager.createNamedQuery("professorPorNome");
         query.setParameter("nomeProfessor", professor + "%");
         return query.getResultList();
    }
     }



